﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wyse.Server.Base
{
    /// <summary>
    /// Base message class
    /// </summary>
    public class Message
    {
        /// <summary>
        /// Ctor
        /// </summary>
        public Message()
        {
        }

        /// <summary>
        /// Ctor
        /// </summary>
        public Message(String action, Object data)
        {
            Action = action;
            Parameters = data;
        }

        /// <summary>
        /// Gets the action string
        /// </summary>
        public String Action
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the message's parameters
        /// </summary>
        public Object Parameters
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a avalue indicating if the message has paremeters
        /// </summary>
        public bool HasParameters
        {
            get { return Parameters != null; }
        }

        /// <summary>
        /// Checks if the message has parameters of
        /// a specific type
        /// </summary>
        /// <typeparam name="Type"></typeparam>
        /// <returns></returns>
        public bool HasParametersOfType<T>()
        {
            return Parameters != null && Parameters is T;
        }

        /// <summary>
        /// Gets the parameters cast to a specific type
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetParameters<T>()
        {
            return (T)Parameters;
        }

        /// <summary>
        /// Comapres two messages
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (obj is Message)
            {
                Message msg = obj as Message;

                if (HasParameters != msg.HasParameters)
                    return false;
                if (Action != msg.Action)
                    return false;

                if (HasParameters)
                    return Parameters.Equals(msg.Parameters);
                return true;
            }

            return false;
        }

        /// <summary>
        /// Calculates the message's hashcode
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            if (Action == null)
                return 0;
            int hash = Action.GetHashCode();
            if (HasParameters)
                hash *= Parameters.GetHashCode();
            return hash;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append('{');
            if (Action == null)
                sb.Append("null");
            else
                sb.Append(Action);
            sb.Append('}');

            return sb.ToString();
        }
    }
}
